package com.xsmart.oa.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface AdminPowerSettingDAO {

    /**
     *  查询对应类型的权限
     * @param actionType
     * @return
     */
    @Select("<script>select ACTION_ID,ACTION_CODE,ACTION_DESC,(CASE WHEN action_type = '0' THEN '平台权限' WHEN action_type = '1' THEN ' 商家权限' END)action_type from CFGS_ACTION <where><if test=\"actionType!=null\">action_type=#{actionType}</if></where> order by action_id </script>")
    List<Map<String, Object>> queryActionDescs(Pagination page,@Param("actionType")Integer actionType);

    /**
     * 获取子分类
     * @param parentId
     * @return
     */
    @Select("<script>SELECT * FROM CFGS_ACTION WHERE <if test=\"parentId==null\">PARENT_ACTION_ID=1</if><if test=\"parentId!=null\">PARENT_ACTION_ID=#{parentId}</if></script>")
    List<Map<String, Object>> queryCfgsAction(@Param("parentId") Long parentId);
    /**
     * 查询权限说明
     * @param actionId
     * @return
     */
    @Select("select action_code from CFGS_ACTION where action_id=#{actionId}")
    String queryActionDescById(@Param("actionId") Integer actionId);
    /**
     * 查询指定类型的管理员权限的id
     * @param
     * @return
     */
    @Select("<script>select action_id from infu_admin_action " +
            "where user_id=#{userId} order by action_id" +
            "</script>")
    List<Integer> queryActionIdByUserId(@Param("userId") Integer userId);

    /**
     * 查询商家所拥有的权限
     * @param userId
     * @param id
     * @return
     */
    @Select("<script>select action_id from infu_admin_seller_action " +
            "where user_id=#{userId} and id=#{id} order by action_id" +
            "</script>")
    List<Integer> queryActionIdByUserId2(@Param("userId") Integer userId,@Param("id")Integer id);
    /**
     * 查询指定的管理员
     * @param page
     * @param userId
     * @return
     */
    @Select("<script>select " +
            "user_id," +
            "user_name," +
            "password," +
            "action_list," +
            "nav_list," +
            "(case when role_id='0' then '平台管理员' when role_id='1' then '供应商管理员' else '运营人员' end)role_id " +
            "from INFS_ADMIN_USER  " +
            "where " +
            "user_id=#{userId}" +
            "</script>")
    List<Map<String, Object>> queryAdminPowerList(Pagination page, @Param("userId") Integer userId);

    /**
     * @param actionList
     * @param userId
     * @return
     */
    @Update("update INFS_ADMIN_USER set action_list=#{actionList} where user_id=#{userId}")
    int updateAdminPower(@Param("actionList") String actionList, @Param("userId") Integer userId);

    /**
     * @param adminMap
     * @param
     * @return
     */
    @Insert("<script>insert into " +
            "<if test=\"functionType=='GLYPTQXPZ'\">infu_admin_action </if>" +
            "<if test=\"functionType=='GLYSJGLQXPZ'\">infu_admin_seller_action </if> " +
            "(user_id," +
            "<if test=\"sellerId!=null\">id,</if>"+
            "action_id," +
            "mod_time," +
            "mod_user_id) " +
            "values(" +
            "#{adminMap.userId}," +
            "<if test=\"sellerId!=null\">#{sellerId},</if>"+
            "#{adminMap.actionId}," +
            "now()," +
            "#{adminMap.modUserId})</script>")
    int insertAdminPower(@Param("adminMap") Map<String, Object> adminMap,@Param("sellerId")Integer sellerId,@Param("functionType")String functionType);

    /**
     * 删除管理员的权限
     * @param adminMap
     * @param
     * @return
     */
    @Delete("<script>" +
            "delete from " +
            "<if test=\"functionType=='GLYPTQXPZ'\">infu_admin_action </if>" +
            "<if test=\"functionType=='GLYSJGLQXPZ'\">infu_admin_seller_action </if> " +
            "where user_id=#{adminMap.userId} and action_id=#{adminMap.actionId}" +
            "<if test=\"sellerId!=null\"> and id=#{sellerId}</if>"+
            "</script>")
    int deleteAdminPower(@Param("adminMap") Map<String, Object> adminMap,@Param("sellerId")Integer sellerId,@Param("functionType")String functionType);

    /**
     * 查询对应管理员可管理的所有商家
     * @param page
     * @param userId
     * @return
     */
    @Select("SELECT * FROM seller_shopinfo where id in(select id from infu_admin_seller_rel where user_id=#{userId})")
    List<Map<String,Object>>queryAllSeller(Pagination page,@Param("userId")Integer userId);


    /**
     * 查询所有的商家
     * @param page
     * @param id
     * @param shopName
     * @return
     */
    @Select("<script>select * from seller_shopinfo <where><if test=\"id!=null and id!=''\">id=#{id}</if><if test=\"shopName!=null and shopName!=''\"> or shop_name=#{shopName}</if></where></script>")
    List<Map<String,Object>>allSeller(Pagination page,@Param("id")Integer id,@Param("shopName")String shopName);

    /**
     * 查询对应管理员可管理的商店id
     * @param id
     * @return
     */
    @Select("select id from infu_admin_seller_rel where user_id=#{userId}")
    List<Integer> queryRelId(@Param("userId")Integer id);
    /**
     * 更新管理员可管理的商家
     * @param id
     * @param user_id
     * @return
     */
    @Insert("insert into infu_admin_seller_rel(id,user_id)values(#{id},#{user_id})")
    int insertInfuAdminSellerRel(@Param("id")Integer id,@Param("user_id")Integer user_id);

    /**
     * 删除管理员可管理商家的商家
     * @param userId
     * @return
     */
    @Delete("delete from infu_admin_seller_rel where user_id=#{userId}")
    int deleteInfuAdminSeller(@Param("userId")Integer userId);

    /**
     * 查询所有的权限编码
     * @param id
     * @return
     */
    @Select("select action_code from cfgs_action where ACTION_ID in(select ACTION_ID from infu_admin_action where user_id=#{id})")
    List<String>queryActionCode(@Param("id")Integer id);
}
